﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using GrapeCity.Forguncy.CellTypes;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;
using GrapeCity.Forguncy.Plugin;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for NoticeBarDrawingObject.xaml
    /// </summary>
    public partial class NoticeBarDrawingObject : UserControl
    {
        public NoticeBarDrawingObject(NoticeBar noticeBar, CellTypeViewModelStyle cellStyle, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var viewModel = new NoticeBarDrawingObjectViewModel();
            cellStyle.Foreground = cellStyle.Foreground ?? drawingHelper.GetBrush("#ed6a0c");
            if (noticeBar.scrollMode != "none")
            {
                cellStyle.TextWrapping = TextWrapping.NoWrap;
            }
            else
            {
                viewModel.TextTrimming = TextTrimming.CharacterEllipsis;
            }
            if(noticeBar.leftIcon != null)
            {
                var leftIcon = noticeBar.leftIcon?.Clone();
                if (leftIcon.UseCellTypeForeColor)
                {
                    leftIcon.Color = cell.ForegroundStr ?? "#ed6a0c";
                }
                viewModel.LeftIcon = IconHelper.GetIcon(leftIcon, drawingHelper);
            }
            if (noticeBar.mode == "closeable")
            {
                viewModel.RightIcon = IconHelper.GetIcon(new ImageValue()
                {
                    BuiltIn = true,
                    Color = cell.ForegroundStr ?? "#ed6a0c",
                    Name = "Mobile/456_delete_8.svg"
                }, drawingHelper);
            }
            else if (noticeBar.mode == "link")
            {
                viewModel.RightIcon = IconHelper.GetIcon(new ImageValue()
                {
                    BuiltIn = true,
                    Color = cell.ForegroundStr ?? "#ed6a0c",
                    Name = "Mobile/467_right_2.svg"
                }, drawingHelper);
            }
            var text = cell.Value?.ToString();
            if(noticeBar.scrollMode == "horizontal")
            {
                text = text?.Replace("\r\n", " ")?.Replace("\r", " ")?.Replace("\n", " ");
            }
            else
            {
                text = text?.Split('\n')?.FirstOrDefault()?.TrimEnd();
            }
            viewModel.Text = text;
            viewModel.CellStyle = cellStyle;
            this.DataContext = viewModel;
            InitializeComponent();
        }

        public class NoticeBarDrawingObjectViewModel : ModelBase
        {
            public string Text { get; set; }
            public Visibility LeftIconVisibility { get { return LeftIcon == null ? Visibility.Collapsed : Visibility.Visible; } }
            public Visibility RightIconVisibility { get { return RightIcon == null ? Visibility.Collapsed : Visibility.Visible; } }
            public object LeftIcon { get; set; }
            public object RightIcon { get; set; }
            public CellTypeViewModelStyle CellStyle { get; set; }

            public TextTrimming TextTrimming { get; set; } = TextTrimming.None;
        }
    }
}
